<?php
/**
 * Created by 王跃达.
 * User: 小马出行
 * Date: 2017/8/24
 * Time: 16:17
 */
namespace app\inter\logic;

use app\inter\model\AreaModel;
use app\admin\inter_model\CarBanModel;
use app\inter\model\CarListModel;
use app\inter\model\DriverCodeModel;
use app\inter\model\DriverMesModel;
use app\inter\model\LoginLogDriverModel;
use app\inter\model\DriverModel;
use app\inter\model\OrderCommonModel;
use think\Cache;
use think\Db;
use think\Model;

class DriverLogic extends Model
{

    public function __construct()
    {
        parent::__construct();

    }

    /**
     * 添加司机工作日志
     * @param string
     * @param int
     * @param string
     * @param string
     * @return string 成功时返回
     */
    public function addWorkLog($did,$is_goon,$start_address='',$end_address=''){
        $data['driver_id'] = $did;
        $data['car_id'] = $this->getInfoByDid($did,'a.id')['id'];
        $data['is_goon'] = $is_goon;
        $data['start_address'] = $start_address;
        $data['end_address'] = $end_address;
        $data['created_time'] = date('Y-m-d H:i:s',time());
        return Db::table('inter_driver_work_log')->insert($data);
    }

    /**
     * 由driver_id获取汽车,司机信息
     * @param $did
     * @param $field
     * @return array|false
     * */
    public function getInfoByDid($did,$field='a.*')
    {
        $car_model = new CarListModel();
        $map['b.driver_id'] = $did;
        return $car_model->getCarList($map,$field,false);
    }

    /**
     * 根据条件获取汽车，司机信息
     * @param $map
     * @param $field
     * @param $flag //true返回二维数组，false返回一维数组
     * @param $order
     * @param $limit
     * @param $page
     * @return array|false
     * */
    public function getCarList($map,$field='a.*',$flag=true,$order = 'a.car_id desc',$page=1,$limit=10)
    {
        $car_model = new CarListModel();
        return $car_model->getCarList($map,$field,$flag,$order,$page,$limit);
    }

    /**
     * 添加汽车信息
     * @param $data
     * @return  array|false
     * */
    public function createCarList($data)
    {
        $car_model = new CarListModel();
        return $car_model->addCarList($data);
    }

    /**
     * 更新汽车信息
     * @param $map
     * @param $data
     * @return string|false
     * */
    public function renewCarList($map,$data)
    {
        $car_model = new CarListModel();
        return $car_model->updateCarList($map,$data);
    }

    /**
     * 计算失败次数
     * @param $driver_info
     * @return  string|false
     */
    public function freezeAccount($driver_info)
    {
        $data['id']=$driver_info["driver_id"];
        $data['error_time']=time();
        if($data['error_time']-$driver_info['error_time']>300){
            $driver_info['error_count']=0;
        }
        $data['error_count']=$driver_info['error_count']+1;
        if($data['error_count']>=5){
            $data['can_time']=time()+3600;
        }
        return $this->renewDriver(array(),$data);
    }

    /**
     * 司机登录
     * @param $driver_info
     * @return array
     */
    public function driverLogin($driver_info)
    {
        $data['id']=$driver_info['driver_id'];
        $data['login_time']=time();
        $data['token']=md5(date("YmdHis",time()).rand(10000,99999));
        /**********登录错误记录归零****************/
        $data['error_count'] = 0;
        $data['error_time'] = 0;
        $data['can_time'] = 0;
        /**********登录错误记录归零****************/
        $this->renewDriver(array(),$data);
        unset($data['id']);
        unset($data['error_count']);
        unset($data['error_time']);
        unset($data['can_time']);
        $data['driver_id'] = $driver_info['driver_id'];
        return $data;
    }

    /**
     * 添加司机登录日志
     * @param $data
     * @return string|false
     * */
    public function createLog($data)
    {
        $log_model = new LoginLogDriverModel();
        return $log_model->createLog($data);
    }

    /**
     * 获取司机修改密码记录
     * @param $map
     * @param $field
     * @param $flag //true返回二维数组，false返回一维数组
     * @param $limit
     * @return array|false
     * */
    public function getCodeRecode($map,$field='*',$flag=true,$limit=10)
    {
        $code = new DriverCodeModel();
        return $code->getCodeRecode($map,$field,$flag,$limit);
    }

    /**
     * 添加司机修改密码记录
     * @param $data
     * @return string|false
     * */
    public function createCode($data)
    {
        $code = new DriverCodeModel();
        return $code->addCode($data);
    }

    /**
     * 更新密码修改记录
     * @param $map
     * @param $data
     * @return string|false
     * */
    public function renewCode($map,$data)
    {
        $code = new DriverCodeModel();
        return $code->updateCode($map,$data);
    }

    /**
     * 获取运营地区信息
     * @param $map
     * @param $field
     * @param $flag //true返回二维数组，false返回一维数组
     * @param $page
     * @param $limit
     * @return array|false
     * */
    public function getArea($map,$field='*',$flag=true,$page=1,$limit=10)
    {
        $area_model = new AreaModel();
        return $area_model->getAreaInfo($map,$field,$flag,$page,$limit);
    }

    /**
     * 根据运营区域id获取运营地区信息
     * @param $arean_id
     * @param $field
     * @return array|boolean
     * */
    public function getAreaByAreaId($arean_id,$field='*')
    {
        $area_model = new AreaModel();
        $map['id'] = $arean_id;
        return $area_model->getAreaInfo($map,$field,false);
    }

    /**
     * 根据运营区域id获取运营地区信息
     * @param $arean_id
     * @return int|boolean|null
     * */
    public function getRegionIdByAreaId($arean_id)
    {
        $result = $this->getAreaByAreaId($arean_id,'province_id,city_id,area_id');
        if($result['area_id']){
            return $result['area_id'];
        }
        return $result['city_id'];
    }

    /**
     * 添加司机信息
     * @param $data
     * @return string|false
     * */
    public function createDriver($data)
    {
        $new = new DriverModel();
        return $new->addDriver($data);
    }

    /**
     * 获取司机信息
     * @param $map
     * @param $field
     * @param $flag //true返回二维数组，false返回一维数组
     * @param $limit
     * @param $page
     * @return array|false
     * */
    public function getDriverInfo($map,$field='*',$flag=true,$page=1,$limit=10)
    {
        $new = new DriverModel();
        return $new->getDriver($map,$field,$flag,$page,$limit);
    }

    /**
     * 更新司机信息
     * @param $map
     * @param $data
     * @return string|false
     * */
    public function renewDriver($map,$data)
    {
        $new = new DriverModel();
        return $new->updateDriver($map,$data);
    }

    /**
     * 获取路线分组信息
     * @param $map
     * @param $field
     * @param $group
     * @return array|false
     * */
    public function getBanciGroup($map,$field='*',$group='')
    {
        if(empty($group)){
            return false;
        }
        $car_banci = new CarBanModel();
        return $car_banci->getCarBanciGroup($map,$field,$group);
    }

    /**
     *  根据订单改变司机状态
     * @param string 
     * @param string
     * @param int
     * @param int
     * @return boolean|int
     */
    public function driverStatus($order_status_now,$order_status_old,$is_goon,$did){
        $order_common_model = new OrderCommonModel();
        $map1['driver_id'] = $map2['driver_id'] = $did;
        $map1['order_status'] = $order_status_now;
        $map2['order_status'] = $order_status_old;
        $res1 = $order_common_model->getOrderCommon($map1,'*',false);
        $res2 = $order_common_model->getOrderCommon($map2,'*',false);
        if($res1 && !$res2){
            $data['is_goon'] = $is_goon;
            $data['car_time'] = time();
            Cache::set('driver_status_'.$did,$is_goon,0);//改变缓存中的司机状态
            if($is_goon==2){
                $ori = $this->getInfoByDid($did,'start_address,end_address');
                $data_car['start_address'] = $ori['end_address'];
                $data_car['end_address'] = $ori['start_address'];
                Cache::rm('order_driver_'.$did);//司机状态已经变为排队中，没有了订单和司机关系缓存
                $this->renewCarList(array('driver'=>$did),$data_car);
            }
            return  $this->renewDriver(array('id'=>$did),$data);
        }
        return false;
    }

    /**
     * 获取司机消息信息
     * @param $map
     * @param $field
     * @param $flag //true返回二维数组，false返回一维数组
     * @param $page
     * @param $limit
     * @return array|false
     * */
    public function getDriverMessage($map=array(),$field='a.*',$flag=true,$page=1,$limit=10)
    {
        $new = new DriverMesModel();
        return $new->getDriverMes($map,$field,$flag,$page,$limit);
    }

    /**
     * 更新司机消息信息
     * @param $map
     * @param $data
     * @return array|false
     * */
    public function renewDriverMesRelation($map,$data)
    {
        $new = new DriverMesModel();
        return $new->updateDriverMesRelation($map,$data);
    }

}